<template>
  <el-container class="layout-container">
    <el-aside width="240px">
      <el-menu
        :router="true"
        class="el-menu-vertical"
        :default-active="$route.path"
        background-color="#ffffff"
        text-color="#333333"
        active-text-color="#67C23A">
        <el-menu-item index="/">
          <el-icon><HomeFilled /></el-icon>
          <span>仪表盘</span>
        </el-menu-item>
        <el-menu-item index="/surveys">
          <el-icon><Document /></el-icon>
          <span>问卷列表</span>
        </el-menu-item>
        <el-menu-item index="/create">
          <el-icon><Plus /></el-icon>
          <span>创建问卷</span>
        </el-menu-item>
        <el-menu-item index="/results">
          <el-icon><DataLine /></el-icon>
          <span>问卷结果</span>
        </el-menu-item>
        <!-- <el-menu-item index="/user">
          <el-icon><User /></el-icon>
          <span>个人信息</span>
        </el-menu-item> -->
        <el-menu-item index="/manage">
          <el-icon><User /></el-icon>
          <span>用户管理</span>
        </el-menu-item>
      </el-menu>
    </el-aside>
    <el-container>
      <el-header>
        <div class="header-left">
          <h2>问卷系统</h2>
        </div>
        <div class="header-right">
          <el-dropdown trigger="click" @command="handleCommand">
            <div class="user-info">
              <el-avatar :size="36" :src="userAvatar" />
              <span class="username">{{ username }}</span>
            </div>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item command="profile">
                  <el-icon><User /></el-icon>个人信息
                </el-dropdown-item>
                <el-dropdown-item command="psd">
                  <el-icon><Setting /></el-icon>修改密码
                </el-dropdown-item>
                <el-dropdown-item divided command="logout">
                  <el-icon><SwitchButton /></el-icon>退出登录
                </el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
      </el-header>
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
    
    <!-- 修改密码对话框 -->
    <el-dialog
      v-model="passwordDialogVisible"
      title="修改密码"
      width="30%"
      :close-on-click-modal="false">
      <el-form :model="passwordForm" :rules="passwordRules" ref="passwordFormRef" label-width="100px">
        <el-form-item label="原密码" prop="oldPassword">
          <el-input v-model="passwordForm.oldPassword" type="password" show-password placeholder="请输入原密码" />
        </el-form-item>
        <el-form-item label="新密码" prop="newPassword">
          <el-input v-model="passwordForm.newPassword" type="password" show-password placeholder="请输入新密码" />
        </el-form-item>
        <el-form-item label="确认密码" prop="confirmPassword">
          <el-input v-model="passwordForm.confirmPassword" type="password" show-password placeholder="请再次输入新密码" />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="passwordDialogVisible = false">取消</el-button>
          <el-button type="primary" @click="submitPasswordChange">确认</el-button>
        </span>
      </template>
    </el-dialog>
  </el-container>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { HomeFilled, Document, Plus, DataLine, User, Setting, SwitchButton } from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'

const router = useRouter()
const userAvatar = ref('https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png')
const username = ref('管理员')
const passwordFormRef = ref(null)
const passwordDialogVisible = ref(false)

// 密码表单数据
const passwordForm = reactive({
  oldPassword: '',
  newPassword: '',
  confirmPassword: ''
})

// 密码表单验证规则
const passwordRules = {
  oldPassword: [
    { required: true, message: '请输入原密码', trigger: 'blur' },
    { min: 6, message: '密码长度不能少于6个字符', trigger: 'blur' }
  ],
  newPassword: [
    { required: true, message: '请输入新密码', trigger: 'blur' },
    { min: 6, message: '密码长度不能少于6个字符', trigger: 'blur' }
  ],
  confirmPassword: [
    { required: true, message: '请再次输入新密码', trigger: 'blur' },
    { min: 6, message: '密码长度不能少于6个字符', trigger: 'blur' },
    {
      validator: (rule, value, callback) => {
        if (value !== passwordForm.newPassword) {
          callback(new Error('两次输入的密码不一致'))
        } else {
          callback()
        }
      },
      trigger: 'blur'
    }
  ]
}

// 提交密码修改
const submitPasswordChange = async () => {
  if (!passwordFormRef.value) return
  
  await passwordFormRef.value.validate(async (valid, fields) => {
    if (valid) {
      try {
        // 调用API进行密码修改
        const { changePassword } = await import('../api/user')
        const result = await changePassword({
          currentPassword: passwordForm.oldPassword,
          newPassword: passwordForm.newPassword
        })
        
        if (result.code === 200) {
          ElMessage.success(result.message || '密码修改成功')
          passwordDialogVisible.value = false
          // 重置表单
          passwordForm.oldPassword = ''
          passwordForm.newPassword = ''
          passwordForm.confirmPassword = ''
        } else {
          ElMessage.error(result.message || '密码修改失败')
        }
      } catch (error) {
        console.error('修改密码出错:', error)
        ElMessage.error('密码修改失败，请稍后重试')
      }
    } else {
      return false
    }
  }, ['oldPassword', 'newPassword', 'confirmPassword']) // 只验证密码相关字段
}

const handleCommand = (command) => {
  switch (command) {
    case 'profile':
      router.push('/user')
      break
    case 'psd':
      // 直接打开修改密码对话框
      passwordDialogVisible.value = true
      break
    case 'logout':
      // 实现退出登录功能
      localStorage.removeItem('token')
      localStorage.removeItem('user')
      router.push('/login')
      break
  }
}
</script>

<style scoped>
.layout-container {
  height: 100vh;
  width: 100vw;
}

.el-aside {
  background-color: #ffffff;
  border-right: 1px solid #e6e6e6;
  box-shadow: 2px 0 8px rgba(0, 0, 0, 0.05);
}

.el-menu {
  border-right: none;
}

.el-menu-item {
  height: 56px;
  line-height: 56px;
  padding: 0 20px;
  font-size: 15px;
}

.el-menu-item .el-icon {
  margin-right: 12px;
  font-size: 18px;
}

.el-menu-item.is-active {
  background-color: #f0f9eb !important;
  color: #67C23A !important;
  font-weight: 500;
}

.el-menu-item:hover {
  background-color: #f0f9eb !important;
}

.el-header {
  background-color: #ffffff;
  border-bottom: 1px solid #e6e6e6;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 24px;
  height: 60px;
}

.header-left h2 {
  color: #67C23A;
  margin: 0;
  font-size: 20px;
  font-weight: 600;
}

.header-right {
  display: flex;
  align-items: center;
}

.user-info {
  display: flex;
  align-items: center;
  cursor: pointer;
  padding: 4px 8px;
  border-radius: 4px;
  transition: background-color 0.3s;
}

.user-info:hover {
  background-color: #f5f7fa;
}

.username {
  margin-left: 8px;
  font-size: 14px;
  color: #606266;
}

:deep(.el-dropdown-menu__item) {
  display: flex;
  align-items: center;
  padding: 8px 16px;
}

:deep(.el-dropdown-menu__item .el-icon) {
  margin-right: 8px;
  font-size: 16px;
}

:deep(.el-dropdown-menu__item.is-divided) {
  margin-top: 6px;
  border-top: 1px solid #ebeef5;
}

.el-main {
  background-color: #f5f7fa;
  padding: 24px;
}
</style>